package com.ccb.ic.bbs.controller;

import com.ccb.ic.bbs.cache.HotTagCache;
import com.ccb.ic.bbs.cache.LoginUserCache;
import com.ccb.ic.query.bbs.PaginationDTO;
import com.ccb.ic.query.bbs.QuestionDTO;
import com.ccb.ic.query.bbs.ResultDTO;
import com.ccb.ic.bbs.provider.UserProvider;
import com.ccb.ic.domain.bbs.User;
import com.ccb.ic.domain.bbs.UserAccount;
import com.ccb.ic.bbs.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IndexController {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private HotTagCache hotTagCache;

    @Autowired
    private LoginUserCache loginUserCache;

    @Autowired
    UserProvider userProvider;

    @GetMapping("/bbs/index")
    public Object index(HttpServletRequest request,
                        @RequestParam(name = "page",defaultValue = "1")Integer page,
                        @RequestParam(name = "size",defaultValue = "15")Integer size,
                        @RequestParam(name = "column", required = false) Integer column2,
                        @RequestParam(name = "search", required = false) String search,
                        @RequestParam(name = "tag", required = false) String tag,
                        @RequestParam(name = "sort", required = false) String sort) {
        User user = userProvider.getUser("token");
        UserAccount userAccount = userProvider.getUserAccount(user);
        List<QuestionDTO> topQuestions = questionService.listTopwithColumn(search, tag, sort,column2);
        PaginationDTO pagination = questionService.listwithColumn(search, tag, sort, page,size,column2,userAccount);
        List<String> tags = hotTagCache.getHots();
        List<User> loginUsers = loginUserCache.getLoginUsers();
        //System.out.println("users"+loginUsers);
        Map<String, Object> model = new HashMap<>();
        model.put("loginUsers", loginUsers);
        model.put("pagination",pagination);
        model.put("search", search);
        model.put("tag", tag);
        model.put("tags", tags);
        model.put("sort", sort);
        model.put("column", column2);
        model.put("topQuestions", topQuestions);
        model.put("navtype", "communitynav");
        return ResultDTO.okOf(model);
    }
}
